package com.example.demo.controller;

import com.example.demo.dto.WorkflowDTO;
import com.example.demo.response.ApiResponse;
import com.example.demo.dto.PageExtra;
import com.example.demo.service.WorkflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/workflows")

public class WorkflowController {

    @Autowired
    private WorkflowService workflowService;


    @GetMapping
    public ResponseEntity<ApiResponse<List<WorkflowDTO>>> getWorkflows(
             @RequestParam(defaultValue = "0") int page,
             @RequestParam(defaultValue = "10") int size,
             @RequestParam(required = false) String flowName,
            HttpServletRequest request) {
        List<WorkflowDTO> workflows = workflowService.getWorkflows(page, size, flowName);
        long total = workflowService.countWorkflows(flowName);
        PageExtra pageExtra = new PageExtra(page, size, total);

        return ResponseEntity.ok(
                ApiResponse.success(workflows, "查询成功", request.getRequestURI(), pageExtra));
    }


    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<WorkflowDTO>> getWorkflowById(
             @PathVariable Long id,
            HttpServletRequest request) {
        WorkflowDTO workflowDTO = workflowService.getWorkflowById(id);
        if (workflowDTO == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(
                ApiResponse.success(workflowDTO, "查询成功", request.getRequestURI(), null));
    }


    @PostMapping
    public ResponseEntity<ApiResponse<WorkflowDTO>> createWorkflow(
            @Valid @RequestBody WorkflowDTO workflowDTO,
            HttpServletRequest httpRequest) {
        WorkflowDTO createdWorkflow = workflowService.createWorkflow(workflowDTO);
        return ResponseEntity.ok(
                ApiResponse.success(createdWorkflow, "创建成功", httpRequest.getRequestURI(), null));
    }


    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<WorkflowDTO>> updateWorkflow(
            @PathVariable Long id,
            @Valid @RequestBody WorkflowDTO workflowDTO,
            HttpServletRequest httpRequest) {
        WorkflowDTO updatedWorkflow = workflowService.updateWorkflow(id, workflowDTO);
        return ResponseEntity.ok(
                ApiResponse.success(updatedWorkflow, "更新成功", httpRequest.getRequestURI(), null));
    }


    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteWorkflow(
             @PathVariable Long id,
            HttpServletRequest request) {
        workflowService.deleteWorkflow(id);
        return ResponseEntity.ok(
                ApiResponse.success(null, "删除成功", request.getRequestURI(), null));
    }


    @GetMapping("/ordered")
    public ResponseEntity<ApiResponse<List<WorkflowDTO>>> getAllWorkflowsOrdered(
            HttpServletRequest request) {
        List<WorkflowDTO> workflows = workflowService.getAllWorkflowsOrdered();
        return ResponseEntity.ok(
                ApiResponse.success(workflows, "查询成功", request.getRequestURI(), null));
    }


    @GetMapping("/order/{workflowOrder}")
    public ResponseEntity<ApiResponse<WorkflowDTO>> getWorkflowByOrder(
            @PathVariable Integer workflowOrder,
            HttpServletRequest request) {
        WorkflowDTO workflowDTO = workflowService.getWorkflowByOrder(workflowOrder);
        if (workflowDTO == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(
                ApiResponse.success(workflowDTO, "查询成功", request.getRequestURI(), null));
    }
}